<template>
    <view>
        <!-- pages/taskManager/taskManager.wxml -->
        <view class="tabs">
            <van-dropdown-menu :active-color="mainColor">
                <van-dropdown-item :value="status" :options="option2" @change="statusDropdown" />
                <van-dropdown-item :value="storeId" :options="stores" @change="storeDropdown" />
            </van-dropdown-menu>
        </view>
        <view class="container">
            <block v-if="list.length">
                <view class="lists">
                    <view class="item" v-for="(item, index) in list" :key="index">
                        <view
                            :class="
                                item.status == 1
                                    ? 'tag blue2'
                                    : item.status == 2
                                    ? 'tag blue2'
                                    : item.status == 3
                                    ? 'tag green'
                                    : item.status == 4
                                    ? 'tag gray'
                                    : item.status == 5
                                    ? 'tag yellow'
                                    : item.status == 6
                                    ? 'tag blue'
                                    : 'tag red'
                            "
                        >
                            {{
                                item.status == 1
                                    ? '已接单'
                                    : item.status == 2
                                    ? '已开始'
                                    : item.status == 3
                                    ? '已完成'
                                    : item.status == 4
                                    ? '已取消'
                                    : item.status == 5
                                    ? '被驳回'
                                    : item.status == 6
                                    ? '已结算'
                                    : '待接单'
                            }}
                        </view>

                        <view class="info">
                            <label>订单编号：</label>
                            <text>{{ item.orderNo }}</text>
                        </view>

                        <view class="info">
                            <label>门店名称：</label>
                            <text>{{ item.storeName }}</text>
                        </view>

                        <view class="info">
                            <label>房间名称：</label>
                            <text>{{ item.roomName }}</text>
                        </view>

                        <view class="info">
                            <label>接单用户：</label>
                            <text v-if="item.userName">{{ item.userName }}</text>
                        </view>

                        <view class="info">
                            <label>预计时间：</label>
                            <text v-if="item.orderEndTime">{{ item.orderEndTime }}</text>
                        </view>

                        <view class="btns">
                            <button hover-class="button-click" class="btn bg-primary" v-if="item.status != 0" @tap="goTaskDetail" :data-info="item.clearId">查看详情</button>
                        </view>
                    </view>
                </view>
                <view v-if="'canLoadMore'" class="noteMore">下拉加载更多...</view>
            </block>
            <block v-else><view class="nodata">暂无任务...</view></block>
        </view>
    </view>
</template>

<script>
// pages/taskManager/taskManager.js
const app = getApp();
var http = require('../../../utils/http');
var util1 = require('../../../utils/util.js');
export default {
    data() {
        return {
            option2: [
                {
                    text: '全部状态',
                    value: ''
                },
                {
                    text: '待接单',
                    value: 0
                },
                {
                    text: '已接单',
                    value: 1
                },
                {
                    text: '已开始',
                    value: 2
                },
                {
                    text: '已完成',
                    value: 3
                },
                {
                    text: '已取消',
                    value: 4
                },
                {
                    text: '被驳回',
                    value: 5
                },
                {
                    text: '已结算',
                    value: 6
                }
            ],
            status: '',
            storeId: '',
            //列表搜索门店id
            stores: [],
            pageNo: 1,
            pageSize: 10,
            canLoadMore: true,
            //是否还能加载更多
            list: [],
            userinfo: {},
            startTime: '',
            endTime: '',
            mainColor: app.globalData.mainColor
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        if (options.status) {
            this.setData({
                status: Number(options.status)
            });
        }
        if (options.startTime) {
            this.setData({
                startTime: options.startTime
            });
        }
        if (options.endTime) {
            this.setData({
                endTime: options.endTime
            });
        }
        this.getuserinfo();
        this.getXiaLaListAdmin();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getMainListdata('refresh');
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        let that = this;
        this.setData({
            pageNo: 1,
            canLoadMore: true,
            list: []
        });
        this.getMainListdata('refresh');
        uni.stopPullDownRefresh();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let that = this;
        if (that.canLoadMore) {
            that.pageNo++;
            this.getMainListdata('');
        } else {
            uni.showToast({
                title: '我是有底线的...'
            });
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        // 获取用户信息
        getuserinfo: function () {
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/user/get',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('我的信息===');
                        console.info(info);
                        if (info.code == 0) {
                            that.setData({
                                userinfo: info.data
                            });
                        }
                    },
                    function fail(info) {}
                );
            } else {
                //console.log('未登录失败！')
            }
        },

        //管理员获取门店下拉列表数据
        getXiaLaListAdmin: function (e) {
            var that = this;
            //if (app.globalData.isLogin)
            {
                http.request(
                    '/member/store/getStoreList',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('下拉门店数据===');
                        console.info(info);
                        if (info.code == 0) {
                            let stores = [];
                            info.data.map((it) => {
                                stores.push({
                                    text: it.key,
                                    value: it.value
                                });
                            });
                            stores.unshift({
                                text: '全部门店',
                                value: ''
                            });
                            that.setData({
                                stores: stores,
                                storeId: stores[0].value
                            });
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        //获取列表数据
        getMainListdata: function (e) {
            var that = this;
            let message = '';
            if (app.globalData.isLogin) {
                if (e == 'refresh') {
                    //刷新，page变为1
                    message = '正在加载';
                    that.setData({
                        pageNo: 1
                    });
                }
                http.request(
                    '/member/manager/getClearManagerPage',
                    '1',
                    'post',
                    {
                        pageNo: that.pageNo,
                        pageSize: that.pageSize,
                        storeId: that.storeId,
                        userId: '',
                        status: that.status,
                        startTime: that.startTime,
                        endTime: that.endTime
                    },
                    app.globalData.userDatatoken.accessToken,
                    message,
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            if (e == 'refresh') {
                                that.setData({
                                    list: info.data.list
                                });
                                if (info.data.list.length === 0) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                            } else {
                                if (info.data != null && info.data.list.length <= info.data.total) {
                                    that.setData({
                                        canLoadMore: false
                                    });
                                }
                                let arr = that.list;
                                let arrs = arr.concat(info.data.list);
                                that.setData({
                                    list: arrs
                                });
                            }
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        //门店下拉菜单发生变化
        storeDropdown(event) {
            //console.log(event)
            this.stores.map((it) => {
                if (it.value === event.detail) {
                    this.setData({
                        storeId: it.value
                    });
                }
            });
            this.getMainListdata('refresh');
        },

        //状态下拉菜单发生变化
        statusDropdown(event) {
            this.option2.map((it) => {
                if (it.value === event.detail) {
                    this.setData({
                        status: it.value
                    });
                }
            });
            this.getMainListdata('refresh');
        },

        // 接单
        take: function (e) {
            let id = e.currentTarget.dataset.info;
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/clear/jiedan/' + id,
                    '1',
                    'post',
                    {
                        id: id
                    },
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            uni.showToast({
                                title: '接单成功'
                            });
                            that.getMainListdata('refresh');
                        } else {
                            uni.showModal({
                                content: info.msg,
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        // 取消接单
        cancelTake: function (e) {
            let id = e.currentTarget.dataset.info;
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/clear/cancel/' + id,
                    '1',
                    'post',
                    {
                        id: id
                    },
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            uni.showToast({
                                title: '取消成功'
                            });
                            that.getMainListdata('refresh');
                        } else {
                            uni.showModal({
                                content: info.msg,
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        // 开始任务
        begin: function (e) {
            let id = e.currentTarget.dataset.info;
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/clear/start/' + id,
                    '1',
                    'post',
                    {
                        id: id
                    },
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            uni.showToast({
                                title: '开始成功'
                            });
                            that.getMainListdata('refresh');
                        } else {
                            uni.showModal({
                                content: info.msg,
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        // 开大门
        open: function (e) {
            let id = e.currentTarget.dataset.info;
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/clear/openStoreDoor/' + id,
                    '1',
                    'post',
                    {
                        id: id
                    },
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            uni.showToast({
                                title: '开大门成功'
                            });
                            setTimeout(() => {
                                that.getMainListdata('refresh');
                            }, 5000);
                        } else {
                            uni.showModal({
                                content: info.msg,
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        // 开房间门
        openDoor: function (e) {
            let id = e.currentTarget.dataset.info;
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/clear/openRoomDoor/' + id,
                    '1',
                    'post',
                    {
                        id: id
                    },
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            uni.showToast({
                                title: '开房间门成功'
                            });
                            setTimeout(() => {
                                that.getMainListdata('refresh');
                            }, 5000);
                        } else {
                            uni.showModal({
                                content: info.msg,
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        // 详情页
        goTaskDetail(e) {
            var id = e.currentTarget.dataset.info;
            uni.navigateTo({
                url: '../taskDetail/taskDetail?id=' + id
            });
        }
    }
};
</script>
<style>
@import './taskManager.css';
</style>
